package com.bsdsma.fengheguaiapp.conf;

import com.bsdsma.fengheguaiapp.enums.Role;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class StringToRoleConverter implements Converter<String, Role> {
    @Override
    public Role convert(String source) {
        if (source == null || source.isEmpty()) {
            return null;
        }
        try {
            return Role.get(Integer.parseInt(source));
        } catch (NumberFormatException e) {
            // 如果不是数字，尝试直接通过名称转换
            return Role.valueOf(source);
        }
    }
}

